<?php  ?>
<?php
include "header.php";
include_once './GCM.php';

?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript">
                    var map;
                    $(document).ready(function() {
            map = new GMaps({
            el: '#map',
                    lat: - 12.043333,
                    lng: - 77.028333
            });
<?php
include_once 'db_functions.php';
$db = new DB_Functions();
$query = $db->getAllGeo();
if ($query != false) {
    $num_row = mysql_num_rows($query);
} else {
    $num_row = 0;
}



$i = 1;

if ($num_row > 0) {
    while ($poly = mysql_fetch_array($query)) {




        echo"   var path = [
                    " . $poly['path'] . "
                ];

              " . $poly['nama_toko'] . " = map.drawPolygon({
                    paths: path,
                    strokeColor: '" . $poly['warna'] . "',
                    strokeOpacity: 1,
                    strokeWeight: 3,
                    fillColor: '" . $poly['warna'] . "',
                    fillOpacity: 0.6
                });
              
               
";


        $i++;
        // }
    }
} else {
    echo "error";
}


$db3 = new DB_Functions();
$query3 = $db3->getAllUsers();
if ($query3 != false) {
    $num_row3 = mysql_num_rows($query3);
} else {
    $num_row3 = 0;
}
if ($num_row3 > 0) {
    ?>
    <?php
    while ($row = mysql_fetch_array($query3)) {
        
        echo "marker".$row['id'] . " = map.addMarker({
                    lat: -12.043333,
                    lng: -77.028333,
                    draggable: true,
                });
              google.maps.event.addListener(marker" . $row['id'] . ", 'dragend', function() {
                    //   updateMarkerStatus('Drag ended');
                    cek(marker" . $row['id'] . ".getPosition(),'".$row['gcm_regid']."');
                        document.getElementById(\"posisi\").innerHTML =marker" . $row['id'] . ".getPosition();
                
                });";      
    }
} else {
    
                                          
 } 


                                        echo"       function cek(latLng,regId) {";
//document.getElementById('info').innerHTML = [latLng.lat(),latLng.lng()].join(', ');
                                                $db2 = new DB_Functions();
                                                $query2 = $db2 -> getAllGeo();
                                                if ($query2 != false) {
                                        $num_row2 = mysql_num_rows($query2);
                                                } else {
                                        $num_row2 = 0;
                                                }
                                        if ($num_row2 > 0) {
                                        //for ($count2 = 0; $count2 <= $num_row2; $count2++) {
                                        while ($poly2 = mysql_fetch_array($query2)) {
                                        echo " if (map.checkGeofence(parseFloat(latLng.lat()), parseFloat(latLng.lng()), ".$poly2['nama_toko'].")) {
                                                alert(\"".$poly2['nama_toko']."\"); loadXMLDoc(regId,'".$poly2['id']."') ";
                                                        // $gcm->send_notification($registatoin_ids, $message);
                                                        echo "}";
                                                       
                                                            
                                                        // $i++;
                                                }
                                                // }
                                                } else {
                                                echo "error";
                                                        }
                                                echo "}";
                                                        ?>
                                                        function loadXMLDoc(id, message)
                                                        {
                                                        var xmlhttp;
                                                                if (window.XMLHttpRequest)
                                                        {// code for IE7+, Firefox, Chrome, Opera, Safari
                                                        xmlhttp = new XMLHttpRequest();
                                                        }
                                                        else
                                                        {// code for IE6, IE5
                                                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                                                        }
                                                        xmlhttp.onreadystatechange = function()
                                                        {
                                                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                                                        {
                                                        document.getElementById("status").innerHTML = xmlhttp.responseText;
                                                        }
                                                        }
                                                        xmlhttp.open("GET", "send_message.php?regId=" + id + "&id=" + message, true);
                                                                xmlhttp.send();
                                                        }
                                                });
        </script>
<div class="row-fluid sortable">
    <div class="span12">
        <div id="map"></div>
    </div>
</div><!--/row-->
<div class="row-fluid sortable">
    <div class="span12">
     <p>Status:</p>
     <div id="status"></div>
     <p>Posisi:</p>
     <div id="posisi"></div>
 </div>
</div>
<?php include "footer.php"; ?>